import {Component, OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Http} from '@angular/http';

@Component({
  selector: 'app-chat-http',
  templateUrl: './chat-http.component.html',
  styleUrls: ['./chat-http.component.css']
})
export class ChatHttpComponent implements OnInit {

  dataSource: Observable<any>;
  // threads: Array<any> = [];
  // users: Array<any> = [];
  messages: Array<any> = [];
  threads: Observable<any>;

  // messages: Observable<any>;

  constructor(private http: Http) {
    // 定义一个请求返回一个Observable<response>，为了在页面上良好的展示，需要将其转换成json格式放在流Observable中
    this.dataSource = this.http.get('/app/getmessages').map((res) => res.json());
    this.threads = this.http.get('/app/getthreads').map((res) => res.json());
    // this.messages = this.http.get('/app/getmessages').map((res) => res.json());
  }

  ngOnInit() {
    // 订阅这个流,get请求由subsribe触发,手工调用
    this.dataSource.subscribe((data) => this.messages = data);

    // 使用异步管道接受流作为输入，自动订阅流 | Async
  }

}
